package cate.game.role.bag.spice;

import cate.common.table.d.GDObj;
import cate.common.table.item.spice.row.*;
import cate.game.GameBody;
import cate.game.attr.FightAttr;
import cate.game.play.support.PlayBuildContext;
import cate.game.role.Role;
import cate.game.role.bag.item.BaseBagItem;
import com.fasterxml.jackson.annotation.JsonIgnore;
import cp.solution.pattern.ICopiable;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;

import java.util.*;

@NoteClass("星格")
public class Spice extends BaseBagItem implements ICopiable<Spice> {
    @NoteField(value = "星格等级")
    public int level;

    @NoteField(value = "星格经验")
    public int exp;

    @NoteField(value = "穿戴着的英雄uid")
    public String onHeroUid;

    @NoteField("是否突破")
    public boolean broken;

    public Spice() {}

    public Spice(Role role, int tid, long num) {
        super(role, GDObj.Type.SPICE, tid,num);
        this.level = 1;
        this.initialize(role);
    }


    @Override
    public Spice copy() {
        Spice cp = new Spice();
        super.copy(cp);
        cp.level = this.level;
        cp.exp = this.exp;
        return cp;
    }

    @Override
    public SpiceBaseRow getTpl(PlayBuildContext ctx) {
        return ctx.spiceExcel().base.get(tid);
    }

    @Override
    public SpiceBaseRow getTpl(GameBody game) {
        return getTpl(game.fight.getPlayBuildContext());
    }

    /**
     * 被消耗时提供的经验
     *
     * @return exp
     */
    @JsonIgnore
    public int getConsumeExp() {
        int expTotal = this.exp;
        SpiceBaseRow row = getTpl(role.toPlayBuildContext());
        if (row != null) {
            expTotal += row.baseExp;
            if(this.level > 0){
                expTotal += role.getGame().table.spice.exp.getExpByQualityAndLevel(row.quality, level);
            }
        }
        return expTotal;
    }

    /**
     * 增加经验
     *
     * @param addExp exp
     * @return 多余的经验
     */
    public int addExp(int addExp) {
        int backExp = 0;
        int quality = getTpl(role.toPlayBuildContext()).quality;
        List<SpiceExpRow> rows = role.getGame().table.spice.exp.getRowsByQualityAndLevel(quality, this.level);
        if (rows == null || rows.isEmpty()) {
            return addExp;
        }
        int tmpLevel = level;
        addExp += exp;
        for(SpiceExpRow row : rows){
            if(addExp >= row.expRequired && row.expRequired > 0){
                addExp -= row.expRequired;
                tmpLevel = row.level + 1;
            }else{
                break;
            }
        }
        exp = addExp;
        level = tmpLevel;
        //升到满级后多余的经验也要返还
        SpiceExpRow maxRow = rows.get(rows.size() - 1);
        if (maxRow != null && level == maxRow.level) {
            backExp = addExp;
            exp = 0;
        }
        return backExp;
    }

    @JsonIgnore
    public FightAttr getPowerAttr(PlayBuildContext ctx){
        FightAttr attr = new FightAttr();

        SpiceBaseRow row = getTpl(ctx);
        if (row == null) {
            return attr;
        }

        // 属性兽魂的属性
        SpiceLevelAttrRow attrRow = ctx.spiceExcel().levelAttr.getAttrRow(tid, level);
        if(attrRow != null){
            attr.add(new FightAttr(attrRow.attrStr));
        } else {
            SpiceBreakAttrRow breakAttrRow = ctx.spiceExcel().breakAttr.getAttrRow(tid, level);
            if (breakAttrRow != null) {
                attr.add(new FightAttr(breakAttrRow.breakAttrStr));
            }
        }
        return attr;
    }

    @Override
    public String toDetail() {
        return "" + level;
    }
}
